import { useEffect, useState, useRef } from "react";
import { ComponentConfigs } from "../data";

const sentMeg = function (params) {
  window.parent &&
    window.parent.postMessage(JSON.parse(JSON.stringify(params)), "*");
};

export default function (callback) {
  useEffect(() => {
    const _postmessage = function (event) {
      try {
        //  if (!_this.$refs.cubeDecorate) return window.removeEventListener('message', _postmessage);
        const data = event.data || {};
        // 数据规范 code, data
        if (data.code && data.data) {
          console.log('data',data);
          if (data.code == "mousemove") {
            //     handleMousemove(data.data);
          }
          if (data.code == "moveholddom") {
            //  _this.isPlaceholder = false;
          }
          if (data.code == "edit") {
            // 编辑
            //   _this.editCallBack(data);
          }
          callback(data);
        }
      } catch (e) {
        console.error("webview message", e);
      }
    };
    // 更改高度
    // setInterval(() => {
    //   sentMeg({ code: 'height', data: document.body.clientHeight + 5 });
    // }, 5000);
    window.addEventListener("message", _postmessage, false);

    // 传递数据配置
    sentMeg({
      code: "config",
      data: ComponentConfigs,
    });
  }, []);

  return { sentMeg };
}
